<!-- 适龄配置编辑弹窗 -->
<template>
  <el-dialog
    :title="'适龄配置'"
    :visible="visible"
    width="67s0px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="220px">
      <el-form-item label="适龄提示icon:">
        <el-radio-group v-model="form.age_icon">
          <el-radio
            :label="'https://cqyqsdk.oss-cn-hangzhou.aliyuncs.com/qnbox/20230831/cb116f86d155e799cc8b08165fc14e98.png'"
          >
            <img
              src="http://juhesdk.oss-accelerate.aliyuncs.com/AgeAppropriateTips/6145b55695d01.png"
            />
          </el-radio>
          <el-radio
            :label="'https://cqyqsdk.oss-cn-hangzhou.aliyuncs.com/qnbox/20230831/0097bc81378f361aeb346d4d31da36fb.png'"
          >
            <img
              src="http://juhesdk.oss-accelerate.aliyuncs.com/AgeAppropriateTips/6145b55695e31.png"
          /></el-radio>
          <el-radio
            :label="'https://cqyqsdk.oss-cn-hangzhou.aliyuncs.com/qnbox/20230831/465787c82fe60444b8bfdf5dfcdd92cc.png'"
          >
            <img
              src="http://juhesdk.oss-accelerate.aliyuncs.com/AgeAppropriateTips/6145b55695ef3.png"
            />
          </el-radio>
          <el-radio
            label=""
          >
          取消
        </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="适龄提示县浮ICON横向百分比:" prop="age_icon_place_x" v-if="form.age_icon">
        <el-input
          v-model="form.age_icon_place_x"
          type="numble"
          :placeholder="'请输入县浮ICON横向百分比'"
        ></el-input>
        <span>0表示最左.1表示最右.0.5表示中间</span>
      </el-form-item>
      <el-form-item label="适龄提示县浮ICON纵向百分比:" prop="age_icon_place_y" v-if="form.age_icon">
        <el-input
          v-model="form.age_icon_place_y"
          type="numble"
          :placeholder="'请输入县浮ICON纵向百分比'"
        ></el-input>
        <span>0表示顶部.1表示底部,0.5表示中间</span>
      </el-form-item>
      <el-form-item label="适龄提示内容链接地址:" v-if="form.age_icon" prop="age_content_url">
        <el-input
          v-model="form.age_content_url"
          :placeholder="'请输入适龄提示内容链接地址'"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'PackageAgeEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {},
  data() {
    return {
      // 表单数据
      form: Object.assign({}),
      // 表单验证规则
      rules: {
        age_icon: [
          { required: true, message: '请选择适龄提示icon', trigger: 'blur' }
        ],
        age_icon_place_x: [
          {
            required: true,
            trigger: 'blur'
          }
        ],

        age_icon_place_y: [
          {
            required: true,
            trigger: 'blur'
          }
        ],
        age_content_url: [
          {
            required: true,
            message: '请输入适龄提示内容链接地址',
            trigger: 'blur'
          }
        ]
      },

      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      age_icon_8:
        'https://cqyqsdk.oss-cn-hangzhou.aliyuncs.com/qnbox/20230831/cb116f86d155e799cc8b08165fc14e98.png',
      age_icon_12:
        'https://cqyqsdk.oss-cn-hangzhou.aliyuncs.com/qnbox/20230831/0097bc81378f361aeb346d4d31da36fb.png',
      age_icon_16:
        'https://cqyqsdk.oss-cn-hangzhou.aliyuncs.com/qnbox/20230831/465787c82fe60444b8bfdf5dfcdd92cc.png',
      // 适龄icon
      packageId: '',
      age_icon_place_x: '',
      age_icon_place_y: '',
      age_content_url: '',
      save_form: {}
      // sdk_version_guanlian: ""
    }
  },
  watch: {
    visible() {
      if (this.data && this.visible) {
        // console.log(this.data.id, '!!DDAA')
        this.packageId = this.data.id

        this.$http
          .get('/Package/getPackageConfig?package_id=' + this.packageId)
          .then((res) => {
            // console.log(res)
            if (res.data.code === 0) {
              res.data.data ? this.form = res.data.data  : console.log(this.form);
            } else {
              this.$message.error(res.data.msg)
            }
          })
          .catch((e) => {
            this.$message.error(e.message)
          })
        this.form = Object.assign({}, this.data.id)
        this.isUpdate = true
      } else {
        this.form = {}
        this.isUpdate = false
      }
    }
  },
  methods: {
    validatex(rule, value, callback) {
      if (!value) {
        callback(new Error('请输入适龄提示县浮ICON横向百分比'))
      } else if (isNaN(value)) {
        callback(new Error('请输入有效数字'))
      } else if (value < 0 || value > 1) {
        callback(new Error('请输入0到1之间的数字'))
      } else if (!/^\d+(\.\d{1,2})?$/.test(value)) {
        callback(new Error('请输入正确的数字格式（最多两位小数）'))
      } else {
        callback()
      }
    },
    validatey(rule, value, callback) {
      if (!value) {
        callback(new Error('请输入适龄提示县浮ICON纵向百分比'))
      } else if (isNaN(value)) {
        callback(new Error('请输入有效数字'))
      } else if (value < 0 || value > 1) {
        callback(new Error('请输入0到1之间的数字'))
      } else if (!/^\d+(\.\d{1,2})?$/.test(value)) {
        callback(new Error('请输入正确的数字格式（最多两位小数）'))
      } else {
        callback()
      }
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        // console.log(this.form, 'ffff')

        this.age_icon_place_x = this.form.age_icon_place_x
        this.age_icon_place_y = this.form.age_icon_place_y
        this.age_content_url = this.form.age_content_url
        this.age_icon = this.form.age_icon

        this.save_form.age_icon = this.age_icon
        this.save_form.package_id = this.packageId
        this.save_form.age_icon_place_x = this.age_icon_place_x
        this.save_form.age_icon_place_y = this.age_icon_place_y
        this.save_form.age_content_url = this.form.age_content_url

        // console.log(this.save_form, 'ffff222')

        if (valid) {
          this.loading = true
          this.form.package_id = this.form.id
          this.$http
            .post('/Package/setPackageAgeConfig', this.save_form)
            .then((res) => {
              this.loading = false
              if (res.data.code === 0) {
                this.$message.success(res.data.msg)
                if (!this.isUpdate) {
                  this.form = {}
                }
                this.updateVisible(false)
                this.$emit('done')
              } else {
                this.$message.error(res.data.msg)
              }
            })
            .catch((e) => {
              this.loading = false
              this.$message.error(e.message)
            })
        }
      })
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value)
      // console.log(this.form, '!!')
    }
  }
}
</script>

<style scoped></style>
